package com.coder520.config;

import com.coder520.common.constants.Parameters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.session.web.http.HeaderHttpSessionStrategy;
import org.springframework.session.web.http.HttpSessionStrategy;

/**
 * @author wsq
 *         Created by wsq on 2018-07-21 17:23.
 * @desc session配置  使用这个必须导入spring-session-data-redis
 * Configuration 相当xml中的bean
 */
@Configuration
@EnableRedisHttpSession
public class HttpSessionConfig {
    @Autowired
    private Parameters parameters;

    /**
     * 采用什么策略
     * @return
     */
    @Bean
    public HttpSessionStrategy httpSessionStrategy(){
        return new HeaderHttpSessionStrategy();
    }

    /**
     * 配置redis链接
     * @return
     */
    @SuppressWarnings("Duplicates")
    @Bean
    public JedisConnectionFactory connectionFactory(){
        JedisConnectionFactory connectionFactory = new JedisConnectionFactory();

        String redisHost = parameters.getRedisNode().split(":")[0];
        int redisPort = Integer.valueOf(parameters.getRedisNode().split(":")[1]);

        connectionFactory.setTimeout(2000);
        connectionFactory.setHostName(redisHost);
        connectionFactory.setPort(redisPort);
        connectionFactory.setPassword(parameters.getRedisAuth());

        return connectionFactory;
    }

}
